/**
 * IP规则管理、白名单、黑名单
 * 
 * @type
 */
var iplist = {
	getId : function() {
		return 'iplist';
	},
	getEditPugin : function() {
		return new Ext.create('Ext.grid.plugin.RowEditing', {
					clicksToEdit : 2,
					autoCancel : true,
					saveBtnText : '保存',
					cancelBtnText : "取消"
				});
	},
	init : function() {
		return this.getView();
	},
	getParams : function() {
		return this.obj;
	},
	action : {
		search : 'search',
		add : 'add',
		del : 'del',
		update : 'update'
	},
	/**
	 * type:类型 action:当前的结构类型
	 * 
	 * @param {}
	 *            obj
	 */
	setParams : function(obj) {
		this.obj = obj;
	},
	getView : function() {
		var me = this;
		// 包裹
		var pp = Ext.create('Ext.panel.Panel', {
					border : false,
					// 新的布局方式
					layout : {
						type : 'hbox',
						align : 'stretch',
						pack : 'start'
					},
					defaults : {
						// 子元素平均分配宽度
						flex : 1
					},
					items : [me.getIpRoleGrid(), me.getIpBlankGrid()]
				});
		return pp;
	},
	getIpRoleGrid : function() {
		var me = this;
		var iprolstore = Ext.create('Ext.data.Store', {
					proxy : {
						type : 'ajax',
						url : 'iplist/UpdateIpListServlet',
						reader : {
							type : 'json',
							root : 'datas'
						}
					},
					fields : ['ruleId', 'ruleFrom', 'ruleTo'],
					listeners : {
						beforeload : function(store) {
							var new_params = {
								type : 'iprole',
								action : 'search'
							};
							Ext.apply(store.proxy.extraParams, new_params);
						}
					}
				})
		iprolstore.reload();
		var rowEditing = me.getEditPugin();
		var selModel = Ext.create('Ext.selection.CheckboxModel');
		var grid = Ext.create('Ext.grid.Panel', {
			title : 'IP规则管理',
			border : false,
			store : iprolstore,
			viewConfig : {
				forceFit : 'true'
			},
			selModel : selModel,
			columns : [{
						header : '起始IP',
						dataIndex : 'ruleFrom',
						flex : 1,
						editor : {
							allowBlank : false
						}
					}, {
						header : '结束IP',
						dataIndex : 'ruleTo',
						flex : 1,
						editor : {
							allowBlank : false
						}
					}],
			frame : true,
			tbar : [{
						text : '新建规则',
						iconCls : 'icon-add',
						handler : function() {
							rowEditing.cancelEdit();
							var r = [{
										ruleFrom : '',
										ruleTo : ''
									}];
							iprolstore.insert(0, r);
							rowEditing.startEdit(0, 0);
						}
					}, {
						itemId : me.getId() + 'remove',
						text : '删除规则',
						iconCls : 'icon-delete',
						handler : function() {
							var sm = grid.getSelectionModel();
							rowEditing.cancelEdit();
							if (sm.getCount() == 0) {
								Ext.Msg.show({
											title : '操作提示',
											msg : '请选择数据再删除！',
											buttons : Ext.Msg.YES,
											icon : Ext.Msg.WARNING
										});
							}
							Ext.MessageBox.confirm("请确认", "您确定要删除选定的 "
											+ sm.getCount() + " 条数据吗？删除后无法恢复！",
									function(button, text) {
										if (button == "yes") {
											var myMask = new Ext.LoadMask(Ext
															.getBody(), {
														msg : '正在添加，请稍后...',
														removeMask : true
													});
											myMask.show();
											var ddList = '';
											for (var i = 0; i < sm.getCount(); i++) {
												ddList = sm.getSelection()[i]
														.get('ruleId')
														+ "_=_" + ddList;
											}
											Ext.Ajax.request({
												url : "iplist/UpdateIpListServlet",
												method : "POST",
												params : {
													ddList : ddList,
													action : 'delete',
													type : 'iprole'
												},
												success : function(response,
														options) {
													if (response.responseText
															&& response.responseText.length > 0) {
														var result = Ext.JSON
																.decode(response.responseText);
														myMask.hide();
														if (result.success) {
															myMask.hide();
															if (result.type == 0) {
																Ext.Msg.show({
																	title : '操作提示',
																	msg : result.msg,
																	buttons : Ext.Msg.YES,
																	icon : Ext.Msg.ERROR
																});
															} else {
																utils
																		.hideAutoNotification(
																				'提示',
																				result.msg);
																iprolstore
																		.reload();
															}
														} else {
															utils
																	.showLoginWin(result.msg);
														}
													}
												},
												failure : function(response,
														options) {
													myMask.hide();
													Ext.Msg.show({
														icon : Ext.MessageBox.ERROR,
														buttons : Ext.Msg.OK,
														title : '错误',
														msg : '删除数据失败!'
													});
												}
											});
										}
									});
						},
						disabled : true
					}],
			plugins : [rowEditing],
			listeners : {
				'selectionchange' : function(view, records) {
					grid.down('#' + me.getId() + 'remove')
							.setDisabled(!records.length);
				},
				'edit' : function(editor, e, eOpts) {
					var myMask = new Ext.LoadMask(Ext.getBody(), {
								msg : '正在添加，请稍后...',
								removeMask : true
							});
					myMask.show();
					var ruleFrom = e.record.get('ruleFrom');
					var ruleTo = e.record.get('ruleTo');
					var ruleId=e.record.get('ruleId');
					Ext.Ajax.request({
								url : 'iplist/UpdateIpListServlet',
								params : {
									ruleFrom : ruleFrom,
									ruleTo:ruleTo,
									ruleId:ruleId,
									type : 'iprole',
									action : 'save'
								},
								success : function(response) {
									var result = Ext
											.decode(response.responseText);
									if (result.success) {
										myMask.hide();
										if (result.type == 0) {
											Ext.Msg.show({
														title : '操作提示',
														msg : result.msg,
														buttons : Ext.Msg.YES,
														icon : Ext.Msg.WARNING
													});
										} else {
											utils.hideAutoNotification('提示',
													result.msg);
											ipbstore.reload();
										}
									} else {
										myMask.hide();
										utils.showLoginWin(result.msg);
									}
								}
							});
				}
			}
		});
		return grid;
	},
	getIpBlankGrid : function() {
		var me = this;
		var ipbstore = Ext.create('Ext.data.Store', {
					fields : ['blankIp'],
					proxy : {
						method : 'post',
						type : 'ajax',
						url : 'iplist/UpdateIpListServlet',
						reader : {
							type : 'json',
							root : 'datas'
						}
					},
					autoLoad : true,
					listeners : {
						beforeload : function(store) {
							var new_params = {
								type : 'ipblank',
								action : 'search'
							};
							Ext.apply(store.proxy.extraParams, new_params);
						}
					}
				})
		var rowEditing = me.getEditPugin();
		var selModel = Ext.create('Ext.selection.CheckboxModel');
		var grid = Ext.create('Ext.grid.Panel', {
			title : 'IP黑名单管理',
			border : false,
			store : ipbstore,
			selModel : selModel,
			viewConfig : {
				forceFit : 'true'
			},
			columns : [{
						header : '黑名单',
						dataIndex : 'blankIp',
						flex : 1,
						editor : {
							allowBlank : false
						}
					}],
			frame : true,
			tbar : [{
						text : '新建黑名单',
						iconCls : 'icon-add',
						handler : function() {
							rowEditing.cancelEdit();
							var r = [{
										blankIp : ''
									}];
							ipbstore.insert(0, r);
							rowEditing.startEdit(0, 0);
						}
					}, {
						itemId : me.getId() + 'removeblank',
						text : '删除黑名单',
						iconCls : 'icon-delete',
						handler : function() {
							var sm = grid.getSelectionModel();
							rowEditing.cancelEdit();
							if (sm.getCount() == 0) {
								Ext.Msg.show({
											title : '操作提示',
											msg : '请选择数据再删除！',
											buttons : Ext.Msg.YES,
											icon : Ext.Msg.WARNING
										});
							}
							Ext.MessageBox.confirm("请确认", "您确定要删除选定的 "
											+ sm.getCount() + " 条数据吗？删除后无法恢复！",
									function(button, text) {
										if (button == "yes") {
											var myMask = new Ext.LoadMask(Ext
															.getBody(), {
														msg : '正在添加，请稍后...',
														removeMask : true
													});
											myMask.show();
											var ddList = '';
											for (var i = 0; i < sm.getCount(); i++) {
												ddList = sm.getSelection()[i]
														.get('blankIp')
														+ "_=_" + ddList;
											}
											Ext.Ajax.request({
												url : "iplist/UpdateIpListServlet",
												method : "POST",
												params : {
													ddList : ddList,
													action : 'delete',
													type : 'ipblank'
												},
												success : function(response,
														options) {
													if (response.responseText
															&& response.responseText.length > 0) {
														var result = Ext.JSON
																.decode(response.responseText);
														myMask.hide();
														if (result.success) {
															myMask.hide();
															if (result.type == 0) {
																Ext.Msg.show({
																	title : '操作提示',
																	msg : result.msg,
																	buttons : Ext.Msg.YES,
																	icon : Ext.Msg.ERROR
																});
															} else {
																utils
																		.hideAutoNotification(
																				'提示',
																				result.msg);
																ipbstore
																		.reload();
															}
														} else {
															utils
																	.showLoginWin(result.msg);
														}
													}
												},
												failure : function(response,
														options) {
													myMask.hide();
													Ext.Msg.show({
														icon : Ext.MessageBox.ERROR,
														buttons : Ext.Msg.OK,
														title : '错误',
														msg : '删除数据失败!'
													});
												}
											});
										}
									});
						},
						disabled : true
					}],
			plugins : [rowEditing],
			listeners : {
				'selectionchange' : function(view, records) {
					grid.down('#' + me.getId() + 'removeblank')
							.setDisabled(!records.length);
				},
				'edit' : function(editor, e, eOpts) {
					var myMask = new Ext.LoadMask(Ext.getBody(), {
								msg : '正在添加，请稍后...',
								removeMask : true
							});
					myMask.show();
					var blankIp = e.record.get('blankIp');
					Ext.Ajax.request({
								url : 'iplist/UpdateIpListServlet',
								params : {
									blankIp : blankIp,
									type : 'ipblank',
									action : 'save'
								},
								success : function(response) {
									var result = Ext
											.decode(response.responseText);
									if (result.success) {
										myMask.hide();
										if (result.type == 0) {
											Ext.Msg.show({
														title : '操作提示',
														msg : result.msg,
														buttons : Ext.Msg.YES,
														icon : Ext.Msg.WARNING
													});
										} else {
											utils.hideAutoNotification('提示',
													result.msg);
											ipbstore.reload();
										}
									} else {
										myMask.hide();
										utils.showLoginWin(result.msg);
									}
								}
							});
				}
			}
		});
		return grid;
	}
}